home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Icon Display / IconMenus.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.6 KB  |  172 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        IconMenus.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "IconMenus.h"
  24. #include <MacTypes.h>
  25. #include <Menus.h>
  26. #include <QDOffscreen.h>
  27. #include <Windows.h>
  28. #include <ToolUtils.h>
  29. #include <Devices.h>
  30. #include <Sound.h>
  31.  
  32. WindowPtr bullseyeWindow;
  33. GWorldPtr    GgwPtr;
  34.  
  35. static void enable(MenuHandle menu, short item, Boolean ok);
  36. void SetUpMenus();
  37. void AdjustMenus();
  38. void HandleMenu (long mSelect);
  39.  
  40. MenuHandle    appleMenu, fileMenu, editMenu;
  41.  
  42. enum    {
  43.     appleID = 1,
  44.     fileID,
  45.     editID,
  46.     widthID
  47.     };
  48.  
  49. enum    {
  50.     openItem = 1,
  51.     closeItem,
  52.     quitItem = 4
  53.     };
  54.  
  55. /****
  56.  * SetUpMenus()
  57.  *
  58.  *    Set up the menus. Normally, we’d use a resource file, but
  59.  *    for this example we’ll supply “hardwired” strings.
  60.  *
  61.  ****/
  62.  
  63. void SetUpMenus()
  64.  
  65. {
  66.     InsertMenu(appleMenu = NewMenu(appleID, "\p\024"), 0);
  67.     InsertMenu(fileMenu = NewMenu(fileID, "\pFile"), 0);
  68.     InsertMenu(editMenu = NewMenu(editID, "\pEdit"), 0);
  69.     DrawMenuBar();
  70.     AppendResMenu(appleMenu, 'DRVR');
  71.     AppendMenu(fileMenu, "\pOpen/O;Close/W;(-;Quit/Q");
  72.     AppendMenu(editMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  73. }
  74. /* end SetUpMenus */
  75.  
  76.  
  77. /****
  78.  *  AdjustMenus()
  79.  *
  80.  *    Enable or disable the items in the Edit menu if a DA window
  81.  *    comes up or goes away. Our application doesn't do anything with
  82.  *    the Edit menu.
  83.  *
  84.  ****/
  85.  
  86. void AdjustMenus()
  87. {
  88.     register WindowPeek wp = (WindowPeek) FrontWindow();
  89.     short kind = wp ? wp->windowKind : 0;
  90.     Boolean DA = kind < 0;
  91.     
  92.     enable(editMenu, 1, DA);
  93.     enable(editMenu, 3, DA);
  94.     enable(editMenu, 4, DA);
  95.     enable(editMenu, 5, DA);
  96.     enable(editMenu, 6, DA);
  97.     
  98.     enable(fileMenu, openItem, !((WindowPeek) bullseyeWindow)->visible);
  99.     enable(fileMenu, closeItem, DA || ((WindowPeek) bullseyeWindow)->visible);
  100. }
  101.  
  102.  
  103. static void enable(MenuHandle menu, short item, Boolean ok)
  104. {
  105.     if (ok)
  106.         EnableItem(menu, item);
  107.     else
  108.         DisableItem(menu, item);
  109. }
  110.  
  111.  
  112. /*****
  113.  * HandleMenu(mSelect)
  114.  *
  115.  *    Handle the menu selection. mSelect is what MenuSelect() and
  116.  *    MenuKey() return: the high word is the menu ID, the low word
  117.  *    is the menu item
  118.  *
  119.  *****/
  120.  
  121. void HandleMenu (long mSelect)
  122. {
  123.     int            menuID = HiWord(mSelect);
  124.     int            menuItem = LoWord(mSelect);
  125.     Str255        name;
  126.     GrafPtr        savePort;
  127.     WindowPeek    frontWindow;
  128.     
  129.     switch (menuID)
  130.       {
  131.       case    appleID:
  132.         GetPort(&savePort);
  133.         GetMenuItemText(appleMenu, menuItem, name);
  134.         OpenDeskAcc(name);
  135.         SetPort(savePort);
  136.         break;
  137.     
  138.       case    fileID:
  139.         switch (menuItem)
  140.           {
  141.           case    openItem:
  142.             ShowWindow(bullseyeWindow);
  143.             SelectWindow(bullseyeWindow);
  144.             break;
  145.                                 
  146.           case    closeItem:
  147.             if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  148.               break;
  149.               
  150.             if (frontWindow->windowKind < 0)
  151.               CloseDeskAcc(frontWindow->windowKind);
  152.             else if (frontWindow = (WindowPeek) bullseyeWindow)
  153.               HideWindow(bullseyeWindow);
  154.                         break;
  155.                         
  156.           case    quitItem:
  157.               DisposeGWorld(GgwPtr);
  158.             ExitToShell();
  159.             break;
  160.           }
  161.         break;
  162.                   
  163.       case    editID:
  164.         if (!SystemEdit(menuItem-1))
  165.           SysBeep(5);
  166.         break;
  167.         
  168.       }
  169. }
  170. /* end HandleMenu */
  171.  
  172.